package com.ruoyi.user.service.impl;

import com.ruoyi.user.domain.NearbyMerchant;
import com.ruoyi.user.domain.MerchantDetail;
import com.ruoyi.user.domain.MerchantProduct;
import com.ruoyi.user.domain.Users;
import com.ruoyi.user.mapper.*;
import com.ruoyi.user.domain.CouponTemplate;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.user.service.INearbyMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 附近商家查询服务实现
 */
@Service
public class NearbyMerchantServiceImpl implements INearbyMerchantService {

    @Autowired
    private NearbyMerchantMapper nearbyMerchantMapper;
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private UserRegionBindingsMapper userRegionBindingsMapper;
    @Autowired
    private CouponTemplateMapper couponTemplateMapper;
    @Autowired
    private UserOrdersMapper userOrdersMapper;
    @Autowired
    private UserProductMapper userProductMapper;

    @Override
    public List<NearbyMerchant> listByRegionId(Long regionId) {
        if (regionId == null ) {
            return Collections.emptyList();
        }
        List<NearbyMerchant> list = nearbyMerchantMapper.selectNearbyMerchantsByRegionId(regionId);
        if (list != null && !list.isEmpty()) {
            for (NearbyMerchant m : list) {
                if (m.getMerchantId() != null && !m.getMerchantId().trim().isEmpty()) {
                    try {
                        Long mid = Long.valueOf(m.getMerchantId());
                        List<CouponTemplate> coupons = couponTemplateMapper.selectAvailableCouponsByMerchantId(mid);
                        Integer orderCount = userOrdersMapper.countOrdersByMerchantLastMonth(mid);
                        m.setCoupons(coupons);
                        m.setOrderCount(orderCount);
                    } catch (NumberFormatException ignored) { }
                }
            }
        }
        return list;
    }





    @Override
    public List<NearbyMerchant> listByRegionAndType(String regionId, String type) {
        if (regionId == null || regionId.trim().isEmpty() || type == null || type.trim().isEmpty()) {
            return Collections.emptyList();
        }
        List<NearbyMerchant> list = nearbyMerchantMapper.selectNearbyMerchantsByRegionAndType(regionId, type);
        if (list != null && !list.isEmpty()) {
            for (NearbyMerchant m : list) {
                if (m.getMerchantId() != null && !m.getMerchantId().trim().isEmpty()) {
                    try {
                        Long mid = Long.valueOf(m.getMerchantId());
                        List<CouponTemplate> coupons = couponTemplateMapper.selectAvailableCouponsByMerchantId(mid);
                        m.setCoupons(coupons);
                    } catch (NumberFormatException ignored) { }
                }
            }
        }
        return list;
    }

    @Override
    public MerchantDetail getDetailByMerchantId(Long merchantId) {
        if (merchantId == null ) {
            return null;
        }
        NearbyMerchant merchant = nearbyMerchantMapper.selectMerchantById(merchantId);
        if (merchant == null) {
            return null;
        }
        List<MerchantProduct> products = userProductMapper.selectProductsByMerchantId(merchantId);
        List<CouponTemplate> coupons = java.util.Collections.emptyList();
        Integer orderCount = userOrdersMapper.countOrdersByMerchantLastMonth(merchantId);
        try {
            if (merchant.getMerchantId() != null && !merchant.getMerchantId().trim().isEmpty()) {
                Long mid = Long.valueOf(merchant.getMerchantId());
                List<CouponTemplate> query = couponTemplateMapper.selectAvailableCouponsByMerchantId(mid);
                if (query != null) {
                    coupons = query;
                }
            }
        } catch (NumberFormatException ignored) {

        }
        MerchantDetail detail = new MerchantDetail();
        detail.setMerchantId(merchant.getMerchantId());
        detail.setMerchantName(merchant.getMerchantName());
        detail.setAvatarUrl(merchant.getAvatarUrl());
        detail.setStatus(merchant.getStatus());
        detail.setRating(merchant.getRating());

        detail.setProducts(products);
        detail.setCoupons(coupons);
        detail.setOrderCount(orderCount);
        return detail;
    }



    @Override
    public Long regionIdByCurrentUser() {
        Long userId = SecurityUtils.getUserId();
        if (userId == null) {
            return 0L;
        }
        Users u = usersMapper.selectCustomerByUserId(userId);
        if (u == null || u.getCustomerId() == null) {
            return 0L;
        }
        Long regionId = userRegionBindingsMapper.selectPrimaryRegionIdByCustomerId(u.getCustomerId());
        if (regionId == null ) {
            return 0L;
        }
        return regionId;
    }


}
